package com.kayden.book.controller;

import com.kayden.book.constant.Constants;
import com.kayden.book.enums.BookStatusEnum;
import com.kayden.book.model.*;
import com.kayden.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

//    @RequestMapping("/getList")
//    public List<BookInfo> getList(){
//        //理应从数据库中查询图书信息，但还未学习数据库
//
//        return bookService.getList();
//    }



    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书，request:{}",bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
        || !StringUtils.hasLength(bookInfo.getAuthor())
        || bookInfo.getCount() == null
        || bookInfo.getPrice() == null
        || !StringUtils.hasLength(bookInfo.getPublish())
        || bookInfo.getStatus() == null){
            log.warn("添加图书的参数不合法，request:{}",bookInfo);
            return "参数不合法";

        }
        try{
            bookService.addBook(bookInfo);
            return "";
        }catch (Exception e){
            log.error("添加图书异常，e:"+e);
            return "添加图书发生异常";
        }
    }


    @RequestMapping("/getListByPage")
    public Result getListByPage(PageRequest pageRequest){
        //        //参数校验
//        //返回数据
//        if (session.getAttribute(Constants.SESSION_USER_KEY) == null){
//            return Result.unlogin();
//        }
//        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
//        if (userInfo == null || userInfo.getId()<=0){
        //用户未登录
//            return Result.unlogin();
//        }
        ResponseResult<BookInfo> listByPage = bookService.getListByPage(pageRequest);
        //return listByPage;
        return Result.success(listByPage);
    }

//    @RequestMapping("/queryBookById")
//    public BookInfo queryBookById(Integer bookId){
//        log.info("查询图书信息，bookId:"+bookId);
//        return bookService.queryBookByInd(bookId);
//    }
    @RequestMapping("/queryBookById")
    public Result<ResponseResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session){
        if (session.getAttribute(Constants.SESSION_USER_KAY) == null){
            return Result.unlogin();
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KAY);
        if (userInfo == null || userInfo.getId() < 0){
            return Result.unlogin();
        }
        ResponseResult<BookInfo> listByPage = bookService.getListByPage(pageRequest);
        return  Result.success(listByPage);
    }

    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("修改图书，bookInfo{}"+bookInfo);
        try{
            bookService.updateBook(bookInfo);
            return "";
        }catch (Exception e){
            log.error("修改图书发生异常，e:"+e);
            return "修改图书异常";
        }
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        log.info("删除图书，bookId:"+bookId);
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            bookService.updateBook(bookInfo);
            return "";
        }catch (Exception e){
            log.error("删除图书发生异常e:",e);
            return "删除图书发生异常";
        }
    }


    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除图书,ids:{}",ids);
        try{
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败,e:",e);
            return false;
        }
    }

}
